$(function() {
    toggleLoading(true);
    var getParams = getArgs();
    var currentHotel = getParams.hotel;
    var roomType = getParams.roomType;
    var startTime = getParams.startTime;
    var hour = getParams.hour;
    var symbols = $('#doc').data('symbols');

    $('#roomGallery').carousel({
        interval: 2000
    });
    $('#roomGallery').on('slid.bs.carousel', function() {
        $('html,body').trigger('scroll');
    });
    toastr.options = {
        "newestOnTop": false,
        "timeOut": 1500,
        "extendedTimeOut": 0,
        "escapeHtml": true,
        "positionClass": "toast-bottom-center"
    }; 
    if (currentHotel === undefined || currentHotel === '') {
        toastr.error(lang('toastr.grogshop'));
        return;
    }
    if (roomType === undefined || roomType === '') {
        toastr.error(lang('toastr.charmber'));
        return;
    }
    if (startTime === undefined || startTime === '') {
        toastr.error(lang('toastr.time'));
        return;
    }
    if (hour === undefined || hour === '') {
        toastr.error(lang('toastr.live'));
        return;
    }    
    var originalHref = $('#header a').attr('data-href');
    $('#header a').attr('href', originalHref + '?hotel=' + currentHotel);

    ajaxRequest('/Home/Room/hourRoomTypeDetail', {
        'hotel': currentHotel,
        'roomType': roomType,
        'startTime': startTime,
        'hour': hour
    }, 'post', function(data) {
        var roomTypeInfo = data.data;
        $(document).attr('title', roomTypeInfo.hotelName);
        $('#doc-header .navbar-title').html(roomTypeInfo.hotelName);
        // 初始化轮转图册
        var carousel = $('#roomGallery').data('gallrry',roomTypeInfo.gallery);
        carousel.find('.inner-item').attr('data-original', roomTypeInfo.gallery[0]).lazyload();
        carousel.find('.carousel-caption').html(roomTypeInfo.roomTypeName);
        var carouselHTML = '';
        var carouselIndicatorHTML = '';
        var templateCarousel = $($('#templateCarouselItem').clone().html());
        var templateCarouselIndicator = $($('#templateCarouselIndicator').clone().html());
        $.each(roomTypeInfo.gallery, function(key, value) {
            if (key === 0) {
                return true;
            }
            templateCarousel.find('.inner-item').attr('data-original', value);
            templateCarouselIndicator.attr('data-slide-to', key);
            carouselHTML += templateCarousel.prop('outerHTML');
            carouselIndicatorHTML += templateCarouselIndicator.prop('outerHTML');
        });
        carousel.find('.carousel-inner').append(carouselHTML);
        $('.carousel .inner-item').lazyload({
            skip_invisible: true
        });
        carousel.find('.carousel-indicators').append(carouselIndicatorHTML);
        //查看图片
        $('#roomGallery').on('click','.inner-item',function(){
            var original = $(this).data('original');
            var picArr = [];
            $.each(roomTypeInfo.gallery, function(key, value) {
                if(value == original){
                    picArr.unshift(value);
                }else{
                    picArr.push(value);
                }
            });           
            window.location.href = '/Home/BookPage/indexpic?hotel=' +currentHotel+'&pic='+picArr.join(',');
        });        
        // 初始化房型描述
        $('#doc-body .room-booking-style').removeClass('hidden').html(roomTypeInfo.description === '' ? lang('noDesc') : roomTypeInfo.description);
        if(parseFloat(roomTypeInfo.charityPrice) == 0){
            $('#doc-body .charityPrice-content').addClass('hidden');
        }else{
            $('#doc-body .rummage').html(roomTypeInfo.charityPrice);
            $('#doc-body .charityPrice-content').removeClass('hidden');
        }
        // 0表示预定房间
        var isBookingRoom = parseInt(roomTypeInfo.bookMode)  === 0;
        if (!isBookingRoom) {
            $('#roomBook').on('click', function(e) {
                var submitBtn = $(this);
                if (submitBtn.attr('disabled') !== undefined) {
                    return false;
                }
                window.location.href = '/Home/BookPage/hourbook?hotel=' + currentHotel + '&roomType=' + roomType + '&startTime=' + startTime + '&hour=' + hour;
            });
            $('#roomBook').removeAttr('disabled');
            toggleLoading(false);
            return;            
        }
        $('.room-booking-show').removeClass('hidden');
        $('.checkbox-box-room').removeClass('hidden');
        $('.room-booking-style').css({'padding-bottom':'60px','padding-top':'16px'});
        
        // 初始化房间列表
        var cardStyle = ['',lang('qrcode'),'',lang('qrcode'),''];
        $('.room-price').text(symbols+roomTypeInfo.price)
        var roomsHTML = '';
        $.each(roomTypeInfo.rooms, function(key, room) {
            var templateRoom = $($('#templateRoom').clone().html());
            if (parseInt(room.enable) === 0) {
                templateRoom.addClass('disabled');
                templateRoom.removeClass('dirty');
            } else {
                templateRoom.removeClass('disabled');
                if(parseInt(room.isDirty) === 1){
                    templateRoom.addClass('dirty');
                }else{
                    templateRoom.removeClass('dirty');
                }
            }
            templateRoom.attr('data-value', room.id).find('.dot-ellipsis').html(room.name);
            var styleNum = parseInt(room.cardType);
            if(styleNum == 1 || styleNum == 3){
                templateRoom.addClass('status-qr').attr('data-type',1);
            }else if(styleNum == 2){
                templateRoom.addClass('status-rf').attr('data-type',2);
            }else if(styleNum == 5){
                templateRoom.addClass('status-qrb').attr('data-type',3);
            }
//          templateRoom.find('.cardStyle').html(cardStyle[styleNum]);
            roomsHTML += templateRoom.prop('outerHTML');
        });
        $('#doc-body .room-type-rooms').html(roomsHTML).on('click', '.selectable-block .vertical-middle-block', function() {
            var block = $(this).parents('.selectable-block');
            var lenght = $('#doc-body .selected').length;
            var type = $(this).closest('.selectable-block').data('type');
            if (block.hasClass('disabled')) {
                toastr.warning(lang('noway'));
                return;
            }
            if (block.hasClass('dirty')) {
                toastr.warning(lang('dirty'));
                return;
            }   
            if (block.hasClass('selected')) {
                block.removeClass('selected');
                if(lenght==1){
                    $('.room-price').addClass('hidden');
                    $('#roomBook').attr('disabled',true);
                }
            }else if(lenght < 3){
                block.addClass('selected');
                $('.room-price').removeClass('hidden');
                $('#roomBook').removeAttr('disabled');
            }else{
                toastr.warning(lang('max3'));
            }
            getselectable(type);
        });
        var getselectable =function(type){
            var insertBefore = $('#insertBefore');
            var qrlock = $('#doc-header .qrlock');
            var qrblock = $('#doc-header .qrblock');
            var rfblock = $('#doc-header .rfblock');
            var roomBooking = $('.room-booking-style');
            var isqrlock = false;
            var isqrblock = false;
            var isrfblock = false;            
            if(type == 1){
                qrlock.insertAfter(insertBefore);
            }else if(type == 2){
                rfblock.insertAfter(insertBefore);
            }else if(type == 3){
                qrblock.insertAfter(insertBefore);
            }
            $('#doc-body .room-type-rooms .selectable-block').each(function(){
               var submitBtn= $(this); 
               if(submitBtn.hasClass('status-qr') && submitBtn.hasClass('selected')){
                   isqrlock = true;
               }
               if(submitBtn.hasClass('status-qrb')&& submitBtn.hasClass('selected')){
                   isqrblock = true;
               }
               if(submitBtn.hasClass('status-rf')&& submitBtn.hasClass('selected')){
                   isrfblock = true;
               }
            });
            if(isqrlock){
                qrlock.removeClass('hidden');
            }else{
                qrlock.addClass('hidden');
            }
            if(isqrblock){
                qrblock.removeClass('hidden');
            }else{
                qrblock.addClass('hidden');
            }
            if(isrfblock){
                rfblock.removeClass('hidden');
            }else{
                rfblock.addClass('hidden');
            }
            if(isqrblock && isqrlock && isrfblock ){
                roomBooking.css({'padding-bottom':'160px'});
            }else if((isqrblock && isqrlock) || (isqrblock && isrfblock)|| (isqrlock && isrfblock)){
                roomBooking.css({'padding-bottom':'130px'});
            }else if(isqrblock || isqrlock|| isrfblock){
                roomBooking.css({'padding-bottom':'100px'});
            }else{
                roomBooking.css({'padding-bottom':'60px'});
            }
        }     
        $('#roomBook').on('click', function(e) {
            var submitBtn = $(this);
            if (submitBtn.attr('disabled') !== undefined) {
                return false;
            }
            var roomArr = [];
            var roomId = '';
            $('#doc-body .selectable-block').each(function() {
                var roomObj = $(this);
                if (roomObj.hasClass('selected')) {
                    roomId = roomObj.data('value');
                    roomArr.push(roomId);
                }
            });
            window.location.href = '/Home/BookPage/hourbook?hotel=' + currentHotel + '&roomType=' + roomType + '&startTime=' + startTime + '&hour=' + hour + '&room=' + roomArr;
        });
        toggleLoading(false);
    }, function(error) {
        toggleLoading(false);
        if(error.responseJSON.data=='403222'){
            $('.room-booking-wrong').removeClass('hidden');
        }else{
            toastr.error(typeof error.responseJSON === 'undefined' ? lang('tip.system') : error.responseJSON.info);
        }
    });
});

